package com.zy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.model.system.SysLoginLog;
import com.zy.model.vo.SysLoginLogQueryVo;
import com.zy.system.mapper.SysLoginLogMapper;
import com.zy.system.service.LoginLogService;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @Author Gong
 * @Description
 * @dATE 2023/6/12 15:33
 **/
@Service
public class LoginLogServiceImpl implements LoginLogService {
    @Resource
    private SysLoginLogMapper sysLoginLogMapper;
    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);
        // 日志状态
        sysLoginLog.setStatus(status);
        sysLoginLogMapper.insert(sysLoginLog);
    }

    @Override
    public IPage<SysLoginLog> selectPage(long page, long limit, SysLoginLogQueryVo sysLoginLogQueryVo) {
        Page<SysLoginLog> pageParam = new Page(page, limit);
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd() ;//封装条件
        QueryWrapper<SysLoginLog> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(username)) {
            wrapper.like( "username" ,username) ;
        }
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge( "create_time" ,createTimeBegin) ;
        }
        if(!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le( "create_time" ,createTimeEnd) ;
        }
        Page pageModel = sysLoginLogMapper.selectPage(pageParam, wrapper);
        return pageModel;

    }

}
